<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;

class School extends Controller{
    public function __construct(){
        parent::__construct();
        if (!Session::has('admin')){
            $this->redirect('index/login');
        }
    }

    //查出部门
    public function apartment(){
        $apartment=Db::table('apartment')->paginate(30);
        $this->assign('apartment',$apartment);
        return view();
    }
    //查出专业
    public function major(){
        $major=Db::table('major')->paginate(30);
        $this->assign('major',$major);
        return view();
    }
    //查出班级
    public function classi(){
        $classi=Db::table('classi')->order('uid asc')->paginate(30);
        $this->assign('classi',$classi);
        return view();
    }

    //删除部门
    public function deleteapartment(){
        $id=input('get.id');
        Db::table('apartment')->where('id',$id)->delete();

        $p=$_SERVER["HTTP_REFERER"];
        $this->redirect($p);
    }
    //删除专业
    public function deletemajor(){
        $id=input('get.id');
        Db::table('major')->where('id',$id)->delete();
        $p=$_SERVER["HTTP_REFERER"];
        $this->redirect($p);
    }
    //删除班级
    public function deleteclassi(){
        $id=input('get.id');
        Db::table('classi')->where('id',$id)->delete();
        $p=$_SERVER["HTTP_REFERER"];
        $this->redirect($p);
    }


    //导出部门
    public function excelapartment() {
        $tableName  = "部门表";
        $Cell  = array(
            array('uid','部门ID'),
            array('apartment','部门名称')
        );
        $data=Db::table('apartment')->select();
        $this->PHPExcel($tableName,$Cell,$data);

    }

    //导出专业
    public function excelmajor() {
        $tableName  = "专业表";
        $Cell  = array(
            array('uid','部门ID'),
            array('mid','专业编号'),
            array('name','专业')
        );
        $data=Db::table('major')->select();
        $this->PHPExcel($tableName,$Cell,$data);
    }


    //导出班级
    public function excelclass() {
        $tableName  = "班级表";
        $Cell  = array(
            array('cid','班级编号'),
            array('classname','班级名称'),
            array('grade','年级'),
            array('mid','所属专业'),
            array('uid','所属部门')
        );
        $data=Db::table('classi')->select();
        $this->PHPExcel($tableName,$Cell,$data);
    }

    //导入部门
    public function putapartment(){
       $file = request()->file('file');
      if ($file){
       $info = $file->validate(['size'=>1567800,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
           $exclePath = $info->getSaveName();  //获取文件名
        $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath;
         
          vendor("PHPExcel.PHPExcel");
            vendor("PHPExcel.PHPExcel.PHPExcel");
            vendor("PHPExcel.PHPExcel.Writer.Excel5");
         vendor("PHPExcel.PHPExcel.Writer.Excel2007");
         vendor("PHPExcel.PHPExcel.IOFactory");
          $PHPExcel = new \PHPExcel();
          $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表 格后缀格式
        
          if ($extension == 'xlsx') {
                $objReader =new \PHPExcel_Reader_Excel2007();
                $PHPExcel = $objReader->load($file_name, $encode = 'utf-8');
            }else if($extension == 'xls'){
                $objReader = new \PHPExcel_Reader_Excel5();
                $PHPExcel = $objReader->load($file_name, $encode = 'utf-8');

            } 
        $sheet = $PHPExcel->getSheet(0);
         $highestRow = $sheet->getHighestRow(); // 取得总行数
         for ($i = 2; $i <= $highestRow; $i++) {
                $data['uid'] = $PHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
                $data['apartment'] = $PHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();
                $data['s'] = $PHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();
               Db::table('apartment')->insert($data);
            } 
			 $this->success('上传成功');
    }else{
       $this->error('没有文件上传');
    }
      

    }
    //导入专业
    public function putMajor(){
      $file = request()->file('file');
      if ($file){
       $info = $file->validate(['size'=>1567800,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
           $exclePath = $info->getSaveName();  //获取文件名
        $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath;
         
          vendor("PHPExcel.PHPExcel");
            vendor("PHPExcel.PHPExcel.PHPExcel");
            vendor("PHPExcel.PHPExcel.Writer.Excel5");
         vendor("PHPExcel.PHPExcel.Writer.Excel2007");
         vendor("PHPExcel.PHPExcel.IOFactory");
          $PHPExcel = new \PHPExcel();
          $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表 格后缀格式
        
          if ($extension == 'xlsx') {
                $objReader =new \PHPExcel_Reader_Excel2007();
                $PHPExcel = $objReader->load($file_name, $encode = 'utf-8');
            }else if($extension == 'xls'){
                $objReader = new \PHPExcel_Reader_Excel5();
                $PHPExcel = $objReader->load($file_name, $encode = 'utf-8');

            } 
        $sheet = $PHPExcel->getSheet(0);
         $highestRow = $sheet->getHighestRow(); // 取得总行数
  for ($i = 2; $i <= $highestRow; $i++) {
$data['uid'] = $PHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
$data['mid'] = $PHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();
$data['name'] = $PHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();
Db::table('major')->insert($data); 
}
$this->success('上传成功');
    }else{
       $this->error('没有文件上传');
    }
      
    }

    //导入班级
    public function putclass(){
       $file = request()->file('file');
      if ($file){
       $info = $file->validate(['size'=>1567800,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
           $exclePath = $info->getSaveName();  //获取文件名
        $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $exclePath;
         
          vendor("PHPExcel.PHPExcel");
            vendor("PHPExcel.PHPExcel.PHPExcel");
            vendor("PHPExcel.PHPExcel.Writer.Excel5");
         vendor("PHPExcel.PHPExcel.Writer.Excel2007");
         vendor("PHPExcel.PHPExcel.IOFactory");
          $PHPExcel = new \PHPExcel();
          $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判断导入表 格后缀格式

          if ($extension == 'xlsx') {
                $objReader =new \PHPExcel_Reader_Excel2007();
                $PHPExcel = $objReader->load($file_name, $encode = 'utf-8');
            }else if($extension == 'xls'){
                $objReader = new \PHPExcel_Reader_Excel5();
                $PHPExcel = $objReader->load($file_name, $encode = 'utf-8');

            } 
        $sheet = $PHPExcel->getSheet(0);
         $highestRow = $sheet->getHighestRow(); // 取得总行数
for ($i = 2; $i <= $highestRow; $i++) {
$data['cid'] = $PHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();
$data['classname'] = $PHPExcel->getActiveSheet()->getCell("B" . $i)->getValue();
$data['grade'] = $PHPExcel->getActiveSheet()->getCell("C" . $i)->getValue();
$data['mid'] = $PHPExcel->getActiveSheet()->getCell("D" . $i)->getValue();
$data['uid'] = $PHPExcel->getActiveSheet()->getCell("E" . $i)->getValue();
Db::table('classi')->insert($data);    
}
 $this->success('上传成功');
    }else{
  
       $this->error('没有文件上传');
    }
      
    }

    public function add(){
        if (request()->isGet()){
            $d=input('get.d');
            switch ($d){
                case 1:
                    $this->assign('d',$d);
                    break;
                case 2:
                    $this->assign('d',$d);
                    break;
                case 3:
                    $this->assign('d',$d);
                    break;
            }
        }
        return view();
    }
    public function addapartment(){
       if (request()->post()){
           $data['uid'] = input('post.uid');
           $data['apartment'] = input('post.apartment');
           $data['s'] = input('post.s');
           Db::table('apartment')->insert($data);
           $this->success('添加成功','apartment');
       }
    }
    public function addmajor(){
        if (request()->post()){
            $data['uid'] = input('post.uid');
            $data['mid'] = input('post.mid');
            $data['name'] = input('post.name');
            Db::table('major')->insert($data);
            $this->success('添加成功','major');
        }
    }
    public function addclassi(){
        if (request()->post()){
            $data['cid'] = input('post.cid');
            $data['classname'] = input('post.classname');
            $data['grade'] = input('post.grade');
            $data['mid'] = input('post.mid');
            $data['uid'] = input('post.uid');
            Db::table('classi')->insert($data);
            $this->success('添加成功','classi');
        }
    }

   public function editor(){
        if (request()->isGet()){
            $d=input('get.d');
            $id=input('get.id');
            switch ($d){
                case 1:
                    $this->assign('d',$d);
                    $info=Db::table('apartment')->where('id',$id)->select();
                    $this->assign('info',$info);
                    break;
                case 2:
                    $this->assign('d',$d);
                    $info=Db::table('major')->where('id',$id)->select();
                    $this->assign('info',$info);
                    break;
                case 3:
                    $this->assign('d',$d);
                    $info=Db::table('classi')->where('id',$id)->select();
                    $this->assign('info',$info);
                    break;
            }

        }
        return view();
   }

    public function updateapartment(){
        if (request()->post()){
            $id=input('post.id');
            $data['uid'] = input('post.uid');
            $data['apartment'] = input('post.apartment');
            $data['s'] = input('post.s');
            Db::table('apartment')->where('id',$id)->update($data);
            $this->success('编辑成功','apartment');
        }
    }
    public function updatemajor(){
        if (request()->post()){
            $id=input('post.id');
            $data['uid'] = input('post.uid');
            $data['mid'] = input('post.mid');
            $data['name'] = input('post.name');
            Db::table('major')->where('id',$id)->update($data);
            $this->success('编辑成功','major');
        }
    }
    public function updateclassi(){
        if (request()->post()){
            $id=input('post.id');
            $data['cid'] = input('post.cid');
            $data['classname'] = input('post.classname');
            $data['grade'] = input('post.grade');
            $data['mid'] = input('post.mid');
            $data['uid'] = input('post.uid');
            Db::table('classi')->where('id',$id)->update($data);
            $this->success('编辑成功','classi');
        }
    }

}

